/**
 * @auth Chunning
 * 
 * @purpose A BUTTON HAS 2 STATE, AND WILL TOGGLE WHEN YOU CLICK IT.
 * 
 * @date 2009-3-18
 * 
 * */
package com.components.button
{
	import core.section.Controller;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	[Event(name="button_on", type="com.components.button.ButtonEvents")]
		
	[Event(name="button_off", type="com.components.button.ButtonEvents")]	

	public class ToggleButton extends Controller
	{
		//START...
						
		//PROPERTY...
		private var tar:Sprite;
		
		private var onState:MCButton;
		
		private var offState:MCButton;
		
		private var _state:String;
		
		private var defaultState:String;
		
		public static const ON_STATE:String = "toggle_on";
		
		public static const OFF_STATE:String = "toggle_off";
		
		
		//CONSTRUCTOR...
		public function ToggleButton(target:DisplayObject,defaultState:String = "toggle_off")
		{
		
			tar = target as Sprite;
			
			this.defaultState = defaultState;
		
			super(target);
		
			init();
			
		}
		
		//METHODS...
		
		//init...
		private function init():void
		{
			
			onState = new MCButton(tar.getChildByName("onState") as MovieClip);
			
			offState =  new MCButton(tar.getChildByName("offState") as MovieClip);
						
			configEventListener();
			
			state = defaultState;		
			
		}
		
		//config element event listener...
		private function configEventListener():void
		{
			
			onState.addEventListener(MouseEvent.CLICK,onStateButtonClickHandler);
			
			offState.addEventListener(MouseEvent.CLICK,offStateButtonClickHandler);
			
		}
		
		//handler...
		
		//on state button click...
		private function onStateButtonClickHandler(evt:MouseEvent):void
		{
			
			state = OFF_STATE;
			
			dispatchEvent(new ButtonEvents(ButtonEvents.BUTTON_OFF,offState));
			
		}
		
		//off state button click...
		private function offStateButtonClickHandler(evt:MouseEvent):void
		{
			
			state = ON_STATE;
			
			dispatchEvent(new ButtonEvents(ButtonEvents.BUTTON_ON,onState));
			
		}
		
		
		//set state...
		public function set state(_state:String):void
		{
			
			this._state = _state;
			
			switch(state)
			{
				
				case OFF_STATE:
				
				onState.target.visible = false;
												
				offState.target.visible = true;
				
				onState.target.alpha = 0;
				
				offState.target.alpha = 1;
				
				break;
				
				case ON_STATE:
				
				onState.target.visible = true;
				
				offState.target.visible = false;
				
				onState.target.alpha = 1;
				
				offState.target.alpha = 0;
				
				break;
				
			}
			
		}
		
		//get state...
		public function get state():String
		{
			
			return _state;
			
		}
		
		//set mouse enblabled...
		public function set mouseEnabled(val:Boolean):void
		{
			
			(onState.target as Sprite).mouseEnabled = val;;
			
			(onState.target as Sprite).mouseEnabled = val;
			
		}
		
		//get mouse enalbed...
		public function get mouseEnabled():Boolean
		{
			
			return (onState.target as Sprite).mouseEnabled;
			
		}
		
		//END
	}
}